package com.example;

/**
 * @author T
 * @description
 * @date 2025年03月03日 21:02
 */
public class SimpleExample {

    private static final ThreadLocal<Integer> integerLocal = new ThreadLocal<>();


    public static void setIntegerLocal(){
        integerLocal.set(100);
    }

    public static Integer getIntegerLocal(){
        return integerLocal.get();
    }

    public static void clearIntegerLocal(){
        integerLocal.remove();
    }

    public static void main(String[] args) {
        setIntegerLocal();
        System.out.println("ThreadLocal 放入 元素");
        System.out.println(Thread.currentThread().getName() + "ThreadLocal 取出 元素" + getIntegerLocal());
        Thread thread = new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println(Thread.currentThread().getName() +"ThreadLocal 取出 元素" + getIntegerLocal());
            }
        });
        thread.start();
        //一定要记得清除ThreadLocal ！清除！清除！重要的事情说三遍
        clearIntegerLocal();
    }

}
